import BaseView from "../../../cfw/mvc/BaseView";
import ToiletController from "../ToiletController";
import ToiletManager from "../ToiletManager";
import { MAX_STATE_NUM } from "../../../public/Config";
import BagManager from "../../bag/BagManager";
import SDKManager from "../../../sdk/SDKManager";
import { SDKEventName, BannerDir } from "../../../sdk/SDKConfig";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ToiletRoomView extends BaseView {


    @property(cc.Node)
    tapButton: cc.Node = null;

    @property(cc.Node)
    paper: cc.Node = null;

    @property(cc.Node)
    handle: cc.Node = null;

    @property(cc.Node)
    player: cc.Node = null;

    @property(cc.Node)
    laotou: cc.Node = null;

    @property([cc.Node])
    buttons: cc.Node[] = []

    @property([cc.Node])
    paopao: cc.Node[] = []

    private step: number = 0;
    private clickCount: number = 0;

    protected controller: ToiletController;
    protected model: ToiletManager;
    start() {
        this.model = ToiletManager.instance();
        this.controller = ToiletController.instance();
        let step = this.model.getStep();
        if (step % 2 == 0) {
            this.player.active = true;
            this.laotou.active = false;
            this.tapButton.active = true;
        } else {
            this.player.active = false;
            this.tapButton.active = false;
            this.laotou.active = true;
            cc.tween(this).to(2, {}).call(this.onButtonBackClick.bind(this)).start();
        }
        SDKManager.getChannel().sendEvent(SDKEventName.enterwc, 0)
    }


    onButtonBackClick() {
        this.controller.gotoLobby()
    }

    onButtonTapClick(e: cc.Touch) {
        if (this.step == 0) {
            this.clickCount++;
            BagManager.addCoin(1, e.getLocation())
            if (this.clickCount == 6) {
                if (SDKManager.getChannel().getzs_click_award_num2()) {
                    SDKManager.getChannel().showBanner(0, BannerDir.MID_DOWN)
                    this.setTimeout(() => {
                        // if (cc.isValid(this.node)) {
                        SDKManager.getChannel().hideBanner()
                    }, 2000);
                }
            }
            if (this.clickCount >= 10) {
                this.tapButton.active = false;
                this.model.addNum(MAX_STATE_NUM / 4)
                this.step++;
                this.buttons[0].active = true;
            }
        }

    }

    onButtonPaperClick(e) {
        if (this.step == 1) {
            this.buttons[0].active = false;
            this.step++

            this.model.addNum(5)
            BagManager.addCoin(10, e.getLocation())
            cc.tween(this.paper).to(0.5, { x: this.tapButton.x, y: this.tapButton.y, angle: 90, scale: 0 }).call(this.flyFinish.bind(this)).start()
        }
    }

    flyFinish() {
        this.player.active = false;
        this.buttons[1].active = true;
        this.step++

        // cc.log(' flyFinish step ', this.step)

    }

    onButtonHandleClick(e) {
        if (this.step == 3) {
            cc.tween(this.handle).repeat(2, cc.tween(this.handle).by(0.2, { y: 10 }).by(0.2, { y: -10 })).start();
            this.step++
            this.buttons[1].active = false;
            this.model.addNum(5)
            BagManager.addCoin(10, e.getLocation())
            cc.log(' 结束 ')
            this.paopaoAction(this.paopao[0])
            cc.tween(this).to(0.2, {}).call(() => {
                this.paopaoAction(this.paopao[1])
            }).start();
            cc.tween(this).to(2, {}).call(this.finish.bind(this)).start();
        }
    }

    paopaoAction(paopao: cc.Node) {
        cc.tween(paopao).repeat(10, cc.tween(paopao).by(0.5, { y: paopao.height }).to(0.1, { opacity: 0 }).call(() => {
            paopao.y = paopao.y - paopao.height;
            paopao.opacity = 255;
        })).start()
    }

    finish() {
        this.controller.intoWash()
    }
}
